package com.xuequ.answerme.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.MixQuesAnsBean;

import java.util.List;

/**
 * Created by Shijie on 2015/9/29.
 *
 */
public class AllRecyclerViewAdapter extends RecyclerView.Adapter<AllRecyclerViewAdapter.ViewHolder> {

    private List<MixQuesAnsBean> mDataList;//数据

    private OnItemClickListener mOnItemClickListener;

    private OnRefreshDoneListener mOnRefreshDoneListener;

    public interface OnRefreshDoneListener {
        void onRefreshDone();

        void loadMore();

        void onLoadMoreDone();
    }

    public void setOnRefreshDoneListener(OnRefreshDoneListener mOnRefreshDoneListener) {
        this.mOnRefreshDoneListener = mOnRefreshDoneListener;
    }

    /**
     * 构造方法
     *
     * @param mDataList
     */
    public AllRecyclerViewAdapter(List<MixQuesAnsBean> mDataList) {
        this.mDataList = mDataList;
    }

    /**
     * Item点击接口
     */
    public interface OnItemClickListener {
        void onItemTitleClick(View view, int position);// 点击问题时触发

        void onItemTitleLongClick(View view, int position);

        void onItemContentClick(View view, int position);// 点击答案时触发
    }

    /**
     * 接口绑定
     *
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 返回
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mix_recycler_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mQuestioner.setText(mDataList.get(position).getQuestionBean().getQuestioner().getUsername());
        holder.mTitle.setText(mDataList.get(position).getQuestionBean().getQuestion_title());
        if (!TextUtils.isEmpty(mDataList.get(position).getAnswerBean().getAnswer_content())) {
            holder.mContent.setText(mDataList.get(position).getAnswerBean().getAnswer_content());
        } else {
            holder.mContent.setText("No answers :(");
        }

        /**
         * 设置绑定点击事件
         */
        if (mOnItemClickListener != null) {
            holder.mTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemTitleClick(holder.itemView, holder.getLayoutPosition());
                }
            });

            holder.mTitle.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemTitleLongClick(holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });

            if (!TextUtils.isEmpty(mDataList.get(position).getAnswerBean().getAnswer_content())) {
                holder.mContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemContentClick(holder.itemView, holder.getLayoutPosition());
                    }
                });
            }
        }
        if (mOnRefreshDoneListener != null && position == mDataList.size() - 1) {
            mOnRefreshDoneListener.loadMore();
        }
    }

    /**
     * 返回Item数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    /**
     * 建立ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTitle;
        public TextView mContent;
        public TextView mQuestioner;

        public ViewHolder(View itemView) {
            super(itemView);
            mQuestioner = (TextView) itemView.findViewById(R.id.mix_recycler_item_question_questioner);
            mTitle = (TextView) itemView.findViewById(R.id.mix_recycler_item_question_title);
            mContent = (TextView) itemView.findViewById(R.id.mix_recycler_item_question_answer);
            //背景框
        }
    }

    public void addData(List<MixQuesAnsBean> mixQuesAnsBeans) {
        Log.i("addData", "length " + mixQuesAnsBeans.size());
        for (int i = 0; i < mixQuesAnsBeans.size(); i++) {
            mDataList.add(i, mixQuesAnsBeans.get(i));
            notifyItemInserted(i);
        }
        if (mOnRefreshDoneListener != null) {
            mOnRefreshDoneListener.onRefreshDone();
        }
    }

    public void addOldData(List<MixQuesAnsBean> mixQuesAnsBeans) {
        for (int i = 0; i < mixQuesAnsBeans.size(); i++) {
            mDataList.add(mixQuesAnsBeans.get(i));
            notifyItemInserted(mDataList.size() - 1);
        }
        if (mOnRefreshDoneListener != null) {
            mOnRefreshDoneListener.onLoadMoreDone();
        }
    }

}
